package com.caiyi.financial.nirvana.sms.service;

import com.caiyi.financial.nirvana.sms.base.service.BaseService;
import com.caiyi.financial.nirvana.sms.entity.SmsUserTemplate;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.enums.SmsUserTempleteStatusEnum;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.mapper.SmsUserTemplateMapper;
import com.caiyi.financial.nirvana.sms.querybean.PageBean;
import com.caiyi.financial.nirvana.sms.querybean.UserTemplateQueryBean;
import com.github.pagehelper.PageHelper;
import com.youyu.common.utils.YyAssert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SmsUserTemplateService extends BaseService<SmsUserTemplate> {

    @Autowired
    private SmsUserTemplateMapper smsUserTemplateMapper;

    public int addUserTemplate(SmsUserTemplate smsUserTemplate) {
        YyAssert.isFalse(StringUtils.isBlank(smsUserTemplate.getTemplate()) || StringUtils.isBlank(smsUserTemplate.getAccount()), ReturnCodeEnum.PARAMS_ERROR.code, "账号或模板不能为空");
        YyAssert.isFalse(smsUserTemplate.getTemplate().length() > 1024, ReturnCodeEnum.PARAMS_ERROR.code, "模版过长");
        smsUserTemplate.setTemplate(smsUserTemplate.getTemplate().trim());
        smsUserTemplate.setStatus(SmsUserTempleteStatusEnum.PASS.index);
        List<String> templateList = smsUserTemplateMapper.selectTemplateListByAccount(smsUserTemplate.getAccount());
        YyAssert.isFalse(templateList.contains(smsUserTemplate.getTemplate()), ReturnCodeEnum.PARAMS_ERROR.code, "模板已存在");
        smsUserTemplate.setSize(smsUserTemplate.getTemplate().length());
        int insertCount = this.insertByEntitySelective(smsUserTemplate);
        //redis中添加用户模板
        if (insertCount == 1) {
            String key = String.format(JedisKey.SMS_TEMPLATE, smsUserTemplate.getAccount());
            JedisService.sadd(key, smsUserTemplate.getTemplate());
        } else {
            YyAssert.isFalse(true, ReturnCodeEnum.PARAMS_ERROR.code, "添加失败");
        }
        return insertCount;
    }

    public int deleteUserTemplate(Integer id) {
        YyAssert.isFalse(id == null, ReturnCodeEnum.PARAMS_ERROR.code, "ID不能为空");
        SmsUserTemplate smsUserTemplate = new SmsUserTemplate();
        smsUserTemplate.setId(id);
        smsUserTemplate = this.selectByPrimaryKey(smsUserTemplate);
        int deleteCount = this.deleteByPrimaryKey(smsUserTemplate);
        YyAssert.isFalse(deleteCount == 0, ReturnCodeEnum.PARAMS_ERROR.code, "删除失败");
        //redis中删除用户模板
        if (smsUserTemplate.getStatus() == SmsUserTempleteStatusEnum.PASS.index) {
            String key = String.format(JedisKey.SMS_TEMPLATE, smsUserTemplate.getAccount());
            JedisService.srem(key, smsUserTemplate.getTemplate());
        }
        return deleteCount;
    }

    //审核通过或审核驳回进行更新
    public int updateUserTemplate(Integer id, Integer status, String remark) {
        YyAssert.isFalse(id == null || status == null, ReturnCodeEnum.PARAMS_ERROR.code, "ID或审核状态不能为空");
        YyAssert.isFalse(status != SmsUserTempleteStatusEnum.AUDIT.index && status != SmsUserTempleteStatusEnum.PASS.index && status != SmsUserTempleteStatusEnum.REJECT.index, ReturnCodeEnum.PARAMS_ERROR.code, "状态只能为审核中、审核驳回、审核通过");
        SmsUserTemplate smsUserTemplate = new SmsUserTemplate();
        smsUserTemplate.setId(id);
        smsUserTemplate = this.selectByPrimaryKey(smsUserTemplate);
        int updateCount = smsUserTemplateMapper.updateTemplateById(remark, status, id);
        logger.info("updateUserTemplate id : {}, status : {}, update : {}", id, status, updateCount);
        YyAssert.isFalse(updateCount == 0, ReturnCodeEnum.PARAMS_ERROR.code, "审核签名失败");
        if (status == SmsUserTempleteStatusEnum.PASS.index) {
            String key = String.format(JedisKey.SMS_TEMPLATE, smsUserTemplate.getAccount());
            JedisService.sadd(key, smsUserTemplate.getTemplate());
        }
        return updateCount;
    }

    public PageBean<SmsUserTemplate> getUserTemplateByParams(UserTemplateQueryBean bean) {
        PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
        List<SmsUserTemplate> list = smsUserTemplateMapper.selectUserTemplateListByParams(bean);
        PageBean<SmsUserTemplate> pageBean = new PageBean<>(list);
        return pageBean;
    }


}
